#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : Chestnut
# @time    : 2025/7/9 09:09
# @function: 海鸟后处理软件的总入口
# @version : V1
import os
import tkinter
import tkinter.filedialog, tkinter.ttk


class PostProcesser:
    def __init__(self):
        self.main_window = tkinter.Tk()
        self.main_window.title("海鸟后处理软件包")
        # self.main_window.geometry("400x300")

        self.inner_title_str = tkinter.StringVar()
        self.foot_notice_str = tkinter.StringVar()

        # 控制栏
        self.mainmenu = tkinter.Menu(self.main_window)
        self.main_window.config(menu=self.mainmenu)

        self.main_frm_width = 800
        self.main_win_height = 500


        self.left_frm = tkinter.Frame(self.main_window, width=200, height=self.main_win_height, borderwidth=1, relief="sunken")
        self.main_frm = tkinter.Frame(self.main_window,  borderwidth=1,relief="sunken")
        self.plot_frm = tkinter.Frame(self.main_window,  width=self.main_frm_width,borderwidth=1,relief="sunken")
        self.foot_lab = tkinter.Label(self.main_window, textvariable=self.foot_notice_str,anchor=tkinter.W,borderwidth=1,relief="sunken")
        self.left_frm.grid(row=1, column=0, rowspan=2, sticky=tkinter.NS)
        self.main_frm.grid(row=1, column=1, sticky=tkinter.NSEW)
        self.plot_frm.grid(row=2, column=1, sticky=tkinter.NSEW)
        self.foot_lab.grid(row=3, column=0, columnspan=2, sticky=tkinter.NSEW)

    def start(self):
        self.set_main_frm()
        self.set_plot_frm()
        self.set_left_frm()
        self.set_menubar()
        self.main_window.mainloop()

    def ask_single_file(self):
        file_name = tkinter.filedialog.askopenfilename()
        if file_name:
            self.file_name = file_name
            self.foot_notice_str.set(file_name)
        else:
            self.file = None
            self.foot_notice_str.set("None file selected")

    def ask_multiple_files(self):
        file_list = tkinter.filedialog.askopenfilenames()
        if file_list:
            self.file_list = file_list
            self.foot_notice_str.set(file_list[0] + f"等{len(file_list)}个文件")
        else:
            self.file_list = None
            self.foot_notice_str.set("None file selected")

    def ask_directory(self):
        dir_path = tkinter.filedialog.askdirectory()
        if dir_path:
            self.dir_path = dir_path
            self.foot_notice_str.set(dir_path)
        else:
            self.dir_path = None
            self.foot_notice_str.set("None directory selected")

    def select_file(self,event=None):
        '''
        左侧栏展示可选择
        :param event:
        :return:
        '''
        self.file_list = os.listdir(self.dir_path)
        self.origin_file_name = self.file_list[self.lb.curselection()[0]]
        self.foot_notice_str.set(self.origin_file_name)

    def execute(self):
        '''
        执行计算操作的函数，实际开发过程中，请重写本函数
        :return:
        '''
        pass

    def set_left_frm(self):
        '''
        对左边栏进行设置的函数，实际开发过程中，请重写本函数
        :return:
        '''
        self.lb = tkinter.Listbox(self.left_frm, height=30)
        self.lb.grid(sticky=tkinter.NS)
        self.lb.bind("<Double-Button-1>", self.select_file)

    def set_main_frm(self):
        '''
        对右边栏进行设置的函数，实际开发过程中，请重写本函数
        :return:
        '''
        tkinter.Label(self.main_frm, textvariable=self.inner_title_str, width=70, bg="yellow").grid(row=0, column=0, sticky=tkinter.NSEW)

    def set_plot_frm(self):
        '''
        对绘图栏进行设置的函数，实际开发过程中，请重写本函数
        :return:
        '''
        pass

    def set_menubar(self):
        '''
        对控制栏进行设置的函数，实际开发过程中请重写本函数
        :return:
        '''
        file_menu = tkinter.Menu(self.mainmenu, tearoff=0)
        file_menu.add_command(label="打开文件", command=self.ask_single_file)
        file_menu.add_command(label="打开多个文件", command=self.ask_multiple_files)
        file_menu.add_command(label="打开文件夹", command=self.ask_directory)
        self.mainmenu.add_cascade(label="文件", menu=file_menu)
        self.mainmenu.add_command(label="执行主要操作", command=self.execute)
        self.mainmenu.add_command(label="退出", command=quit)

if __name__ == '__main__':
    myapp = PostProcesser()
    myapp.start()
